﻿module ScanningTests

open NUnit.Framework
open HtmlScanner
open HtmlParser
open DummyHost
open HtmlDom
open DocumentFactory
open UriParsing


[<TestFixture>]
type testScanning() = 

    [<Test>]
    member this.testScanAttributes() =
       //test code
       let html = """a=1 b="hello" c='far"vel' d e=_blank """
       let s = new Scanner(html)
       let atts = scanAttributes(s)
       Assert.AreEqual(5, atts.Length) 
       // test boolean attribute
       Assert.AreEqual((Attribute("d","d")), atts.[3]) 

    [<Test>]
    member this.tagWithAttributes() =
       //test code
       let html = """ start <tag a=1 b="hello" c='far"vel' d e=_blank> end """
       let nodes = scan html 
       Assert.AreEqual(3, nodes.Length) 
       let tag = nodes.[1] :?> StartTag
       Assert.AreEqual("tag", tag.tagName) 
       Assert.AreEqual(5, tag.attributes.Count) 


    [<Test>]
    member this.testScanHtml() =
       //test code
       let html = """
       <title>test</title>
       test
       """
       let nodes = scan html 
       Assert.AreEqual(4, nodes.Length) 
       let titleStartTag = nodes.[0] :?> StartTag
       Assert.AreEqual("title", titleStartTag.tagName) 
       let titleEndTag = nodes.[2] :?> EndTag
       Assert.AreEqual("title", titleEndTag.tagName) 

    [<Test>]
    member this.testScanHtmlComments() =
       //test code
       let html = """<!DOCTYPE html>
       <title>test</title>
       test<!-- test -->
       """
       let nodes = scan html 
       Assert.AreEqual(4, nodes.Length)

    [<Test>]
    member this.testScanScript() =
       //test code
       let html = """<script>var b=c<d;</script>"""
       let nodes = scan html 
       Assert.AreEqual(3, nodes.Length)
       let text = nodes.[1] :?> TextToken
       Assert.AreEqual("var b=c<d;", text.text)


    [<Test>]
    member this.testGtInComment() =
       let html = """
        <!--li></li-->
        """
       let docNode = createDocument (fun doc->()) blankUri (DummyLoader()) (DummyHost())
       do parseHtmlTokens docNode (scan html)
       let dump = dumpNode docNode
       let nodes = scan html 
       Assert.AreEqual(0, nodes.Length)

    [<Test>]
    member this.decodeEntities() =        
       let html = """<b>&#1570;&#1586;&#1605;&#1575;&#1740;&#1588;&#1740;</b>"""
       let nodes = scan html 
       Assert.AreEqual(3, nodes.Length)
       Assert.AreEqual("آزمایشی", nodes.[1].text)

    [<Test>]
    member this.decodeHexEntity() =        
       let html = """&#x263A;"""
       let nodes = scan html 
       Assert.AreEqual(1, nodes.Length)
       Assert.AreEqual("☺", nodes.[0].text)


    [<Test>]
    member this.decodeNamedEntity() =        
       let html = """&lt;x&gt;&AElig;&oslash;&Aring;"""
       let nodes = scan html 
       Assert.AreEqual(1, nodes.Length)
       Assert.AreEqual("<x>ÆøÅ", nodes.[0].text)